/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package klient;

import Serializable.ParsedCmd;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author Marcin
 */
public class Connection {
    public static int SPORT=8870;
    public static String HOST="localhost";
    
    public static String repair(String in){
        return in.replaceAll("\n", "X");
    }
    
    public static String hash(char[] a){
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("SHA-512");
            digest.reset();
            byte[] input = digest.digest(String.valueOf(a).getBytes("UTF-8"));
            input=digest.digest(input);
            return repair(new String(input));   
        } catch (NoSuchAlgorithmException ex) {
            Klient.log("Nieprawidłowe kodowanie");
            return null;
        } catch (UnsupportedEncodingException ex) {
            Klient.log("Niewspierane enkodowanie");
            return null;
        }               
    }
    
    
    public static void Send(PrintWriter pW, String cmd){
        Klient.log("Serwer: "+cmd);
        pW.println(cmd);
        pW.flush();
    }
    
    public static void Send(PrintWriter pW, ParsedCmd cmd){
        Klient.log("Serwer: "+cmd);
        pW.println(cmd.toString());
        pW.flush();
    }
    
    public static void Init(PrintWriter pW){
        pW.println(new ParsedCmd("WUT?").toString());        
        pW.flush();  
    }
}
